package com.exceeddata.vcloud.common.response;

import lombok.Getter;
import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.exceeddata.vcloud.common.tools.JacksonUtil;

/**
 * 描述: 统一分页格式
 * 创建人: 赵兴炎
 * 日期: 2022年8月31日
 */
@Getter
@Setter
public class PageInfo<T> {
	
    //返回的记录集合
    private List<T> rows;

    //分页起始页
    private Integer page;

    //每页记录数
    private Integer size;

    //总记录条数
    private Long total;
    
	public static <T> PageInfo<T> MyBatisPlus(Object object,Class<T> clazz) {
		PageInfo<T> pageInfo = new PageInfo<>();
		String objectToJson = JacksonUtil.ObjectToJson(object);
		Map<?, ?> jsonToHashMap = JacksonUtil.JsonToHashMap(objectToJson);
		
		Object records = jsonToHashMap.get("records");
		List<T> jsonToList = JacksonUtil.jsonToList(JacksonUtil.ObjectToJson(records),clazz);
		pageInfo.setRows(jsonToList);
		
		Object total = jsonToHashMap.get("total");
		pageInfo.setTotal(Long.parseLong(total.toString()));
		
		Object size = jsonToHashMap.get("size");
		pageInfo.setSize(Integer.parseInt(size.toString()));

		Object current = jsonToHashMap.get("current");
		pageInfo.setPage(Integer.parseInt(current.toString()));
		return pageInfo;
	}
	
	public static <T> PageInfo<T> defaultData(Class<T> clazz) {
		PageInfo<T> pageInfo = new PageInfo<>();
		pageInfo.setRows(new ArrayList<>());
		pageInfo.setTotal(0L);
		pageInfo.setSize(0);
		pageInfo.setPage(1);
		return pageInfo;
	}

}
